package com.matchmaking.system.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.matchmaking.system.config.R;
import com.matchmaking.system.domain.SysDictData;
import com.matchmaking.system.listener.SysDictListener;
import com.matchmaking.system.service.SysDictDataService;
import com.matchmaking.system.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("dictData")
@CrossOrigin
public class SysDictDataController {

    @Autowired
    SysDictDataService dictDataService;

    @Autowired
    SysDictTypeService dictTypeService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 字典表类型查询
     * @param
     * @return
     */
    @PostMapping("dictDataList")
    public R dictDataList(String dictLabel,String status,String dictType,Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<SysDictData> list = dictDataService.selectAll(dictLabel,status,dictType);
        PageInfo<SysDictData> sysDictDataPageInfo = new PageInfo<>(list);
        return R.successed(sysDictDataPageInfo);
    }


    /**
     * 插入操作
     * @param dictData
     * @return
     */
    @PostMapping("dictDataAdd")
    public R dictDataAdd(@RequestBody SysDictData dictData){
        redisTemplate.opsForValue().set("data",dictData);
        dictDataService.insertObject(dictData);
        return R.successed("添加成功");
    }

    /**
     * 逻辑删除
     * @param dictCode
     * @return
     */
    @DeleteMapping("delDataById")
    public R delDataById(Long dictCode){
        dictDataService.delById(dictCode);
        return R.successed("删除成功");
    }


    /**
     * 修改
     * @param dictData
     * @return
     */
    @PostMapping("updateDataById")
    public R updateDataById(@RequestBody SysDictData dictData){
        dictDataService.updateObject(dictData);
        return R.successed("修改成功");
    }

    /**
     * 批量删除
     * @param dictCode
     * @return
     */
    @DeleteMapping("delAll")
    public R delAll(String dictCode){
        System.err.println(dictCode);
        String[] split = dictCode.split(",");
        for (String s : split) {
            dictDataService.delAll(s);
        }
        //dictDataService.delAll(dictCode);
        return R.successed("删除成功");
    }

//    /**
//     * 导出
//     * @param response
//     * @throws IOException
//     */
//    @PostMapping("export")
//    public void export(HttpServletResponse response) throws IOException {
//        List<SysDictData> list = dictDataService.selectAllObj();
//        //遍历循环
//        for (SysDictData sysDictData : list) {
//            //将状态字段转换为文字
//            if("正常".equals(sysDictData.getStatus())){
//                sysDictData.setStatus("0");
//            }else if("停用".equals(sysDictData.getStatus())){
//                sysDictData.setStatus("1");
//            }
//        }
//
//        response.setContentType(EasyExcelConfig.RESPONSE_TYPE);
//        response.setCharacterEncoding(EasyExcelConfig.CHARSET);
//        //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
//        response.setHeader("Content-Disposition","attachment;filename=factory.xls");
//        EasyExcel.write(response.getOutputStream(),SysDictData.class).sheet("字典表数据").doWrite(list);
//    }
//
//
//    @PostMapping("importData")
//    public R importData(MultipartFile file) throws IOException {
//        List<SysDictData> list = EasyExcel.read(file.getInputStream())
//        //从注册监听器,可以在这里校验字段
//                .registerReadListener(new SysDictListener())
//                .head(SysDictData.class)
//                .sheet()
//                .headRowNumber(2)
//                .doReadSync();
//        for (SysDictData sysDictData : list) {
//            System.err.println();
//            dictDataService.insertObject(sysDictData);
//        }
//
//        return R.successed("导入成功");
//    }
}
